package com.dragon.sdgp.common.security;

import com.dragon.sdgp.common.exception.UserCountLockException;
import com.dragon.sdgp.entity.SysUser;
import com.dragon.sdgp.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @AUTHOR : Dragon_Yang
 * @File : MyUserDetailsServiceImpl
 * @DESCRIPTION : 自定义UserDetails
 * @DATE : 2023/2/27 21:18
 */
@Service
public class MyUserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private SysUserService userService;

    @Override
    public UserDetails loadUserByUsername(String account) throws UsernameNotFoundException {
        SysUser sysUser = userService.getUserByEMailOrPhone(account);
        if(sysUser == null) {
            throw new UsernameNotFoundException("用户名或密码错误！");
        }else if(sysUser.getIsDelete() == 1){
            throw new UserCountLockException("该用户账号被移除，具体请联系管理员！");
        }
        return new User(account, sysUser.getPassword(), new ArrayList<>());
    }

}
